package com.cn.serverline.lineicp.produce.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cn.serverline.lineicp.produce.entity.TeExceptionRecords;
import com.cn.serverline.lineicp.produce.entity.TeExceptionSecalation;
import com.cn.serverline.lineicp.produce.entity.TeExceptionSecalationDetail;
import com.cn.serverline.lineicp.produce.entity.dto.TeExceptionRecordsAddDto;
import com.cn.serverline.lineicp.produce.entity.dto.TeExceptionRecordsDto;
import com.cn.serverline.lineicp.produce.entity.dto.TeExceptionSecalationAddDto;
import com.cn.serverline.lineicp.produce.entity.dto.TeExceptionSecalationDto;
import com.cn.serverline.lineicp.produce.entity.vo.TeExceptionRecordsVo;
import com.cn.serverline.lineicp.produce.entity.vo.TeExceptionSecalationVo;
import com.cn.serverline.lineicp.produce.mapper.TeExceptionSecalationMapper;
import com.cn.serverline.lineicp.produce.service.ITeExceptionRecordsService;
import com.cn.serverline.lineicp.produce.service.ITeExceptionSecalationDetailService;
import com.cn.serverline.lineicp.produce.service.ITeExceptionSecalationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.serverline.lineicp.workshopPlan.entity.PlanLineAutomatic;
import com.cn.serverline.lineicp.workshopPlan.entity.PlanLineSchedule;
import com.cn.serverline.lineicp.workshopPlan.service.IPlanLineAutomaticService;
import com.cn.serverline.lineicp.workshopPlan.service.IPlanLineScheduleService;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 异常上报表 服务实现类
 * </p>
 *
 * @author 王凯炫
 * @since 2024-08-22
 */
@Service
public class TeExceptionSecalationServiceImpl extends ServiceImpl<TeExceptionSecalationMapper, TeExceptionSecalation> implements ITeExceptionSecalationService {

    @Resource
    private TeExceptionSecalationMapper teExceptionSecalationMapper;
    @Resource
    private ITeExceptionSecalationDetailService teExceptionSecalationDetailService;

    @Resource
    private IPlanLineAutomaticService planLineAutomaticService;

    @Resource
    private IPlanLineScheduleService planLineScheduleService;

    @Resource
    private ITeExceptionRecordsService exceptionRecordsService;

    /**
     * 添加
     *
     * @param teExceptionSecalationAddDto
     */
    @Override
    @Transactional
    public void add(TeExceptionSecalationAddDto teExceptionSecalationAddDto) {
        TeExceptionSecalation secalation = new TeExceptionSecalation();
        BeanUtils.copyProperties(teExceptionSecalationAddDto, secalation);
        secalation.setProcessedTime(new Date());
        secalation.setProcessedStatus(0L);
        this.save(secalation); // 保存实体
        if (CollectionUtils.isNotEmpty(teExceptionSecalationAddDto.getExeptionDetailsId())) {
            List<TeExceptionSecalationDetail> teExceptionSecalationDetails = new ArrayList<>();
            // 保存详情
            for (Long aLong : teExceptionSecalationAddDto.getExeptionDetailsId()) {
                TeExceptionSecalationDetail teExceptionSecalationDetail = new TeExceptionSecalationDetail();
                teExceptionSecalationDetail.setExceptionSecalationId(secalation.getId());
                teExceptionSecalationDetail.setSecalationDetail(aLong);
                teExceptionSecalationDetails.add(teExceptionSecalationDetail);
            }
            teExceptionSecalationDetailService.saveBatch(teExceptionSecalationDetails);
        }
        // 添加记录操作
        TeExceptionRecords records = new TeExceptionRecords();
        records.setExceptionSecalationId(secalation.getId());
        records.setUserId(teExceptionSecalationAddDto.getUserId());
        records.setUserName(teExceptionSecalationAddDto.getUserName());
        records.setProductContents("异常上报");
        exceptionRecordsService.save(records);
        // 更新任务状态
        if (ObjectUtil.isNotNull(teExceptionSecalationAddDto.getTaskId())) {
            //修改部装状态
            if (teExceptionSecalationAddDto.getOperationStage() == 0) {
                PlanLineSchedule schedule = new PlanLineSchedule();
                schedule.setStaterNumber(3L);
                planLineScheduleService.update(schedule, new LambdaQueryWrapper<PlanLineSchedule>()
                        .eq(PlanLineSchedule::getId, teExceptionSecalationAddDto.getTaskId()));
                //修改总装状态
            } else if (teExceptionSecalationAddDto.getOperationStage() == 1)  {
                PlanLineAutomatic automatic = new PlanLineAutomatic();
                automatic.setId(teExceptionSecalationAddDto.getTaskId());
                automatic.setStaterNumber(3L);
                LambdaQueryWrapper<PlanLineAutomatic> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(PlanLineAutomatic::getId, teExceptionSecalationAddDto.getTaskId());
                planLineAutomaticService.update(automatic,queryWrapper);
            }
        }
    }

    /**
     * 查询异常上报
     * @param teExceptionSecalationDto
     * @return
     */
    @Override
    public List<TeExceptionSecalationVo> sel(TeExceptionSecalationDto teExceptionSecalationDto) {
        List<TeExceptionSecalationVo> list = new ArrayList<>();

        // 获取 operationStage 的值
        Long operationStage = teExceptionSecalationDto.getOperationStage();
        Date start = null;
        Date end = null;
        if (teExceptionSecalationDto.getStartTime() != null && teExceptionSecalationDto.getEndTime() != null ) {
            start = DateUtil.parse(teExceptionSecalationDto.getStartTime());
            end = DateUtil.parse(teExceptionSecalationDto.getEndTime());
        }

        // 使用三目运算符进行条件判断并添加结果
        list.addAll(operationStage == null || operationStage == 0 ? teExceptionSecalationMapper.sel(teExceptionSecalationDto, start, end) : new ArrayList<>());
        list.addAll(operationStage == null || operationStage == 1 ? teExceptionSecalationMapper.sele(teExceptionSecalationDto, start, end) : new ArrayList<>());

        return list;
    }

    /**
     * 查询异常记录
     * @param teExceptionRecordsDto
     * @return
     */
    @Override
    public List<TeExceptionRecordsVo> select(TeExceptionRecordsDto teExceptionRecordsDto) {
        return teExceptionSecalationMapper.select(teExceptionRecordsDto);
    }

    /**
     * 添加异常记录
     * @param teExceptionRecordsAddDto
     */
    @Override
    public void detailsAdd(TeExceptionRecordsAddDto teExceptionRecordsAddDto) {
        TeExceptionRecords records = new TeExceptionRecords();
        BeanUtils.copyProperties(teExceptionRecordsAddDto, records);

        // 使用三目运算符设置 制作内容
        records.setProductContents(teExceptionRecordsAddDto.getNum() == 0 ? "异常处理" : "派单处理");

        // 如果 num 为 1，进行特定处理
        if (teExceptionRecordsAddDto.getNum() == 1) {
            teExceptionSecalationMapper.update(
                    new TeExceptionSecalation()
                            .setProcessedTime(DateUtil.parse(teExceptionRecordsAddDto.getProcessedTime()))
                            .setProcessedById(teExceptionRecordsAddDto.getUserId()),
                    new LambdaUpdateWrapper<TeExceptionSecalation>()
                            .eq(TeExceptionSecalation::getId, teExceptionRecordsAddDto.getExceptionSecalationId())
            );
        }
        exceptionRecordsService.save(records);
    }


}
